const app = getApp();
const loginInfo = require('../../login.js');
Page({
  data: {
    userInfo: {},
    start: 1,
    length: 10,
    isloadmore: 1,
    commentlist: [],
    isnodata: 0,
    fromid: '',
    openid: '',
    defaultInfo: [],
    attachurl: app.siteInfo.attachment_url,
    showModalStatus: false,
    isloading: true,
    ishasInfo: false, // 是否授权
  },
  onLoad: function(options) {
    let opts = options || {};
    let userinfo = wx.getStorageSync('userInfo');
    let openid = options.openid || userinfo.openid || wx.getStorageSync('openid') || wx.getStorageSync('OPENID') || '';
    let ishasInfo = false;
    if (userinfo.nickName && userinfo.openid) {
      ishasInfo = true;
    }
    this.setData({
      opts,
      ishasInfo,
      openid,
      fromid: options.pid,
      themData: app.setThemeColor() || false,
    });
    this.loadComment();
    this.loadDefault();
  },
  loadDefault: function() {
    let that = this;
    app.util.request({
      url: 'entry/wxapp/api',
      data: {
        type: 2,
        default: 1,
        id: that.data.fromid,
      },
      module: 'cm_micro_single_page',
      cachetime: '0',
      success(res) {
        that.setData({
          defaultInfo: res.data.data,
        });
      },
      fail() {
        wx.showToast({
          title: '默认数据加载失败！',
        });
      },
    });
  },
  // 获取店铺评价列表
  loadComment: function() {
    let that = this;
    let start = that.data.start;
    let length = that.data.length;
    app.util.request({
      url: 'entry/wxapp/api',
      data: {
        type: 3,
        start: start,
        length: length,
        pid: that.data.fromid,
        openid: that.data.openid,
      },
      module: 'cm_micro_single_page',
      cachetime: '0',
      success(res) {
        if (res.data.data.length < length) {
          that.setData({
            isloadmore: 0,
            isloading: false,
          });
        } else {
          that.setData({
            isloadmore: 1,
            isloading: false,
          });
        }

        let arr = that.data.commentlist;
        let newarr = arr.concat(res.data.data);
        let isnodata = (newarr.length === 0) ? 1 : 0;
        that.setData({
          commentlist: newarr,
          start: start + 1,
          isnodata: isnodata,
        });
      },
      fail() {
        wx.showToast({
          title: '星星设置失败！',
        });
      },
    });
  },
  // 获取用户信息
  getUserInfo: function(user) {
    let userInfos = wx.getStorageSync('userInfo') || {};
    let detail = user && user.detail && user.detail.rawData && JSON.parse(user.detail.rawData) || JSON.parse(JSON.stringify(userInfos));
    userInfos.wxInfo = detail;
    this.setData({
      userInfos,
    });
    user && loginInfo.updataUserInfo(user.detail.rawData);
    console.log('user:', user);
    console.log('userInfos', userInfos);
    userInfos.wxInfo.nickName && userInfos.wxInfo.avatarUrl && wx.redirectTo({
      url: '../addcomment/addcomment?pid=' + this.data.fromid,
    });
  },
  // 发表评论
  addComment: function() {
    let that = this;
    // let userinfo = wx.getStorageSync('user_info');
    // if (!(userinfo.nickName && userinfo.openid)) {
    //  return  this.setData({
    //     isshow: true
    //   })
    // return wx.showModal({
    //   title: '您好',
    //   content: '您未登录，请先登录',
    //   showCancel: true,
    //   cancelText: '取消授权',
    //   confirmText: '确认授权',
    //   success: function(res) {
    //     if (res.confirm) {
    // wx.setStorageSync('history_page', `/ws_micro_single_page/pages/addcomment/addcomment?pid=${that.data.fromid}` )
    // app.util.navigateTo('/ws_micro_single_page/pages/mycenter/mycenter');
    // } else {
    //   wx.navigateBack({});
    // }
    //   },
    //   fail() {
    //     wx.showToast({
    //       title: '评论失败！',
    //     })
    //   }
    // });
    // }
    wx.redirectTo({
      url: '../addcomment/addcomment?pid=' + that.data.fromid,
    });
  },
});